/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rss.app;

import com.logic.reader;
import com.mysql.mysqlEntries;
import com.mysql.mysqlUser;
import com.objects.entry;
import com.objects.user;
import com.vaadin.terminal.ExternalResource;
import com.vaadin.terminal.Resource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Window;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author Dell
 */
public class show_window extends Window{

    
    private rss_reader rssreader;
    private int id;
    private int userID;
    private entry ent=new entry();
    private Button addFavoriteBtn=new Button("Zu Favoriten");
    private Button removeFavoriteBtn=new Button("Von Favoriten löschen");

    public show_window(rss_reader r,int entryid, int userid) throws Exception
    {
        rssreader = r;
        id=entryid;
        userID=userid;
        ent = mysqlEntries.getEntryByID(id);
        this.setName(ent.getEntryname());
        
        this.setWidth("600px");
        this.setHeight("600px");

        Label titleHeader = new Label("<h3>Titel: </h3>",Label.CONTENT_XHTML);
        Label title = new Label(ent.getEntryname());
        Label descriptionHeader = new Label("<h3>Beschreibung: </h3>",Label.CONTENT_XHTML);
        Label description = new Label(ent.getEntrycontents());
        Label urlHeader = new Label("<h3>URL: </h3>",Label.CONTENT_XHTML);
        Label url = new Label(ent.getEntryURL());

        
        
        Button showEntryBtn=new Button("Artikel öffnen");
        showEntryBtn.addListener(new Button.ClickListener() {

            public void buttonClick(ClickEvent event) {
                
                try {
                    
                    URL link = new URL(ent.getEntryURL());
                    ExternalResource r = new ExternalResource(link);
                    
                    open(r,ent.getEntryname());
                } catch (Exception ex) {
                    Logger.getLogger(show_window.class.getName()).log(Level.SEVERE, null, ex);
                }

                
            }
        });
        

        
        addFavoriteBtn.addListener(new Button.ClickListener() {

            public void buttonClick(ClickEvent event) {
                try {
                    if (mysqlEntries.favoriteEntry(id, userID, Boolean.FALSE)) {
                        removeFavoriteBtn.setVisible(true);
                        addFavoriteBtn.setVisible(false);
                        getWindow().showNotification(
                                (String) "Erfolgreich zu Favoriten hinzugefügt",
                                (String) ent.getEntryname(),
                                Notification.TYPE_WARNING_MESSAGE);
                    }
                } catch (Exception ex) {
                    Logger.getLogger(show_window.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        
        
        removeFavoriteBtn.addListener(new Button.ClickListener() {

            public void buttonClick(ClickEvent event) {
                 try {
                    if (mysqlEntries.favoriteEntry(id, userID, Boolean.TRUE)) {
                        removeFavoriteBtn.setVisible(false);
                        addFavoriteBtn.setVisible(true);
                        getWindow().showNotification(
                                (String) "Erfolgreich von Favoriten entfernt",
                                (String) ent.getEntryname(),
                                Notification.TYPE_WARNING_MESSAGE);
                    }
                } catch (Exception ex) {
                    Logger.getLogger(show_window.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });

        addComponent(titleHeader);
        addComponent(title);
        addComponent(descriptionHeader);
        addComponent(description);
        addComponent(urlHeader);
        addComponent(url);
        addComponent(showEntryBtn);
        addComponent(removeFavoriteBtn);
        addComponent(addFavoriteBtn);

        if (mysqlEntries.checkIfEntryIsFavorite(userid, id))
        {
            addFavoriteBtn.setVisible(false);
            
        }
        else
        {
            removeFavoriteBtn.setVisible(false);
        }
 
        
    }

   
}
